package com.gpframework.module.controller.sys;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.sys.domain.SysAnnouncement;
import com.gpframework.module.sys.domain.req.SysAnnouncementAdd;
import com.gpframework.module.sys.domain.req.SysAnnouncementReq;
import com.gpframework.module.sys.domain.req.SysAnnouncementUpdate;
import com.gpframework.module.sys.domain.vo.SysAnnouncementVo;
import com.gpframework.module.sys.service.ISysAnnouncementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName sysAnnouncementController
 * @Author gpframework
 * @Date 2022-08-23
 * @Version 1.0.0
 * @Description 系统公告Controller
 */
@Validated
@Api(tags = "系统公告")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes", "unchecked"})
@RequestMapping("/sys/announcement")
public class SysAnnouncementController extends BaseController {

    private final ISysAnnouncementService isysAnnouncementService;

    /**
     * @Description 分页
     * @Author gpframework
     * @Date 2022-08-23
     * @Param sysAnnouncement
     * @Return TableDataInfo
     */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('sys:announcement:list')")
    @GetMapping("/list")
    public TableDataInfo<SysAnnouncementVo> list(SysAnnouncementReq sysAnnouncementReq) {
        startPage();
        QueryWrapper<SysAnnouncement> lqw = new QueryWrapper<SysAnnouncement>();
        sysAnnouncementReq.generatorQuery(lqw, true);
        List<SysAnnouncementVo> list = isysAnnouncementService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2022-08-23
     * @Param sysAnnouncement
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('sys:announcement:add')")
    @Log(title = "添加系统公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody SysAnnouncementAdd sysAnnouncementAdd) {
        SysAnnouncement sysAnnouncement = Convert.convert(new TypeReference<SysAnnouncement>() {
        }, sysAnnouncementAdd);
        return toAjax(isysAnnouncementService.save(sysAnnouncement) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2022-08-23
     * @Param sysAnnouncement
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('sys:announcement:edit')")
    @Log(title = "修改系统公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody SysAnnouncementUpdate sysAnnouncementUpdate) {
        SysAnnouncement sysAnnouncement = Convert.convert(new TypeReference<SysAnnouncement>() {
        }, sysAnnouncementUpdate);
        return toAjax(isysAnnouncementService.updateById(sysAnnouncement) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2022-08-23
     * @Param sysAnnouncement
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('sys:announcement:export')")
    @Log(title = "导出系统公告", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysAnnouncementReq sysAnnouncementReq) {
        QueryWrapper<SysAnnouncement> lqw = new QueryWrapper<>();
        sysAnnouncementReq.generatorQuery(lqw, true);
        List<SysAnnouncementVo> list = isysAnnouncementService.listVo(lqw);
        ExcelUtil<SysAnnouncementVo> util = new ExcelUtil<>(SysAnnouncementVo.class);
        return util.exportExcel(list, "announcement");
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2022-08-23
     * @Param sysAnnouncement
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('sys:announcement:query')")
    @GetMapping(value = "/{announcementId}")
    public AjaxResult<SysAnnouncementVo> getInfo(@PathVariable("announcementId") Long announcementId) {
        return AjaxResult.success(isysAnnouncementService.getVoById(announcementId));
    }


    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2022-08-23
     * @Param sysAnnouncement
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('sys:announcement:remove')")
    @Log(title = "删除系统公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{announcementIds}")
    public AjaxResult remove(@PathVariable Long[] announcementIds) {
        return toAjax(isysAnnouncementService.removeByIds(Arrays.asList(announcementIds)) ? 1 : 0);
    }
}
